package com.example.robertluozizhao.baseframedemo.db;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;


import com.example.robertluozizhao.baseframedemo.util.LogUtils;
import com.example.robertluozizhao.baseframedemo.util.reflect.ReflectUtils;
import com.example.robertluozizhao.baseframedemo.util.reflect.model.ClsMember;

import java.util.List;


/**
 * Created by Administrator on 2016/7/8 0008.
 */
public class DBHelper extends SQLiteOpenHelper {
    private static final String TAG="DBHelper";
    private static final String DBNAME="stermV1.db";//数据库名称
    private static final int VERSION = 7;//当前数据库版本号
    private static SQLiteDatabase database;

    public static synchronized SQLiteDatabase getDB(Context context){
        if(database==null){
            database=new DBHelper(context).getWritableDatabase();
        }
        return database;
    }


    public DBHelper(Context contex){
        super(contex, DBNAME, null, VERSION);
    }

    @Override
    public void onCreate(SQLiteDatabase db) {

/*        createTable(db, UserBean.class);
        createTable(db, ServerBean.class);
        createTable(db, BluetoothDeviceModel.class);
        createTable(db, LocalLog.class);
        createTable(db, BlockInfo.class);
        createTable(db, BJLogRecord.class);
        createTable(db, LogoBean.class);
        createTable(db, PushMessageBean.class);
        createTable(db, Trip.class);
        createTable(db, LockLog.class);
        createTable(db, ServerAddress.class);
        createTable(db, PhotoInfoBean.class);*/

    }


    /**
     * 创建数据表
     * @param db
     * @param cls
     */
    private void createTable(SQLiteDatabase db, Class<?> cls, String tableName){
        StringBuffer t = new StringBuffer();
        t.append("CREATE TABLE IF NOT EXISTS " + tableName + "(" + "_id INTEGER primary key autoincrement");
        List<ClsMember> objMember= ReflectUtils.getObjectMembers(cls,0);
        for (ClsMember member : objMember) {
            if(member.getType().equals("long")){
                t.append("," + member.getName() + " bigint");
            }else if(member.getType().equals("int")){
                t.append("," + member.getName() + " int");
            }else if(member.getType().equals("short")){
                t.append("," + member.getName() + " smallint");
            }else{
                t.append("," + member.getName() + " text");
            }
        }
        t.append(");");
        db.execSQL(t.toString());
        LogUtils.d(TAG, t.toString());
    }


    /**
     * 创建数据表
     * @param db
     * @param cls
     */
    private void createTable(SQLiteDatabase db, Class<?> cls){
        StringBuffer t = new StringBuffer();
        t.append("CREATE TABLE IF NOT EXISTS " + cls.getSimpleName() + "(" + "_id INTEGER primary key autoincrement");
        List<ClsMember> objMember= ReflectUtils.getObjectMembers(cls,0);
        for (ClsMember member : objMember) {
            if(member.getType().equals("long")){
                t.append("," + member.getName() + " bigint");
            }else if(member.getType().equals("int")){
                t.append("," + member.getName() + " int");
            }else if(member.getType().equals("short")){
                t.append("," + member.getName() + " smallint");
            }else{
                t.append("," + member.getName() + " text");
            }
        }
        t.append(");");
        db.execSQL(t.toString());
        LogUtils.d(TAG, t.toString());
    }

    /**
     * 添加表字段
     * @param db
     * @param tableName
     * @param columnName
     * @param columnType
     */
    private void addColumnInTable(SQLiteDatabase db, String tableName, String columnName, String columnType) {
        String sql = "ALTER TABLE " + tableName + " ADD COLUMN " + columnName + " " + columnType;
        db.execSQL(sql);
    }

    /**
     * 删除表
     * @param db
     * @param table
     */
    private void deleteTable(SQLiteDatabase db, String table) {
        String sql = "DROP TABLE IF EXISTS " + table;
        db.execSQL(sql);
    }
    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

        switch (oldVersion){
/*            case 1:
                createTable(db,PushMessageBean.class);
            case 2:
                createTable(db, Trip.class);
            case 3:
                createTable(db, LockLog.class);
            case V_1_5_3:
                addColumnInTable(db, BluetoothDeviceModel.class.getSimpleName(), "rssi", "int");
            case V_1_6_3:
                createTable(db, ServerAddress.class);
            case V_1_6_3_4:
                createTable(db, PhotoInfoBean.class);
            default:break;*/
        }

    }
}
